package xyz.zhzby.cyzx.servicebase.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import io.swagger.annotations.ApiModel;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author zby
 * @date 2021/12/06
 */
@ApiModel(description = "适用于分模块的项目")
public class CodeGenerator {
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        //获取项目目录
        String parentDir = System.getProperty("user.dir");
        //设置包名路径
        String packagedir = "xyz/zhzby/cyzx/com";
        //设置entity目录
        String entitydir = parentDir + "/service/service_com/src/main/java/" + packagedir + "/entity/";
        //设置mapper目录
        String mapperdir = parentDir + "/service/service_com/src/main/java/" + packagedir + "/mapper/";
        //设置mapper对应xml目录
        String mapperxmldir = parentDir + "/service/service_com/src/main/resources/mapper/";
        //设置service及impl目录
        String servicedir = parentDir + "/service/service_com/src/main/java/" + packagedir + "/service/";
        String serviceImpldir = parentDir + "/service/service_com/src/main/java/" + packagedir + "/service/impl/";

        //代码生成
        AutoGenerator mpg = new AutoGenerator();
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        GlobalConfig gc = new GlobalConfig();
        //是否支持swaggeer
        gc.setSwagger2(true);
        //设置作者
        gc.setAuthor("zby");
        //是否打开资源管理器
        gc.setOpen(false);
        //设置id的生成策略默认算法
        gc.setIdType(IdType.ASSIGN_ID);
        //文件生成覆盖
        gc.setFileOverride(true);
        //日期的类型 用java.util.date 代替
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        //数据库配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/cyzx?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("mysqlroot");
        mpg.setDataSource(dataSourceConfig);

        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("xyz.zhzby.cyzx.com");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        //设置自定义输出目录（分布式项目使用）
        Map<String, String> pathInfo = new HashMap<>(5);
        pathInfo.put(ConstVal.ENTITY_PATH, entitydir);
        pathInfo.put(ConstVal.MAPPER_PATH, mapperdir);
        pathInfo.put(ConstVal.XML_PATH, mapperxmldir);
        pathInfo.put(ConstVal.SERVICE_PATH, servicedir);
        pathInfo.put(ConstVal.SERVICE_IMPL_PATH, serviceImpldir);
        pc.setPathInfo(pathInfo);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix("com_");
        //表名和字段名的生成策略：下划线命名变为驼峰命名 eg: zby_test --> zbyTest
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //设置公共父类
        strategy.setSuperEntityClass("xyz.zhzby.cyzx.common.domain.DataLogInfo");
        strategy.setSuperEntityColumns("create_user", "create_time", "update_user", "update_time", "delete_flag");
        //是否支持Lombok
        strategy.setEntityLombokModel(true);
        //是否添加 @TableField("表名")
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setInclude(scanner("表名，多个表名用英文逗号分割").split(","));
        mpg.setStrategy(strategy);

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
